﻿using System;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
using System.Web;
using System.Globalization;

namespace LastfmNet
{
    internal static class StringExtensions
    {
        public static int ToInteger(this string str)
        {
            int integerValue = 0;
            int.TryParse(str, out integerValue);
            return integerValue;
        }

        public static uint ToUnsignedInteger(this string str)
        {
            uint integerValue = 0;
            if (uint.TryParse(str, out integerValue))
            {
                return integerValue;
            }
            else
            {
                return 0;
            }
        }

        public static bool IsInteger(this string str)
        {
            Regex regularExpression = new Regex("^-[0-9]+$|^[0-9]+$");
            return regularExpression.Match(str).Success;
        }

        public static bool ToBoolean(this string str)
        {
            if (str == "1") return true;
            if (str == "0") return false;
            bool boolValue;
            if (bool.TryParse(str, out boolValue))
            {
                return boolValue;
            }
            else
            {
                return false;
            }
        }

        public static DateTime ToDateTime(this string str)
        {
            DateTime dateTimeValue;
            if (DateTime.TryParse(str, out dateTimeValue))
            {
                return dateTimeValue;
            }
            else
            {
                return DateTime.MinValue;
            }
        }

        public static decimal ToDecimal(this string str)
        {
            decimal decimalValue = 0;
            if (decimal.TryParse(str.Replace(',', '.'), out decimalValue))
            {
                return decimalValue;
            }
            else
            {
                if (decimal.TryParse(str.Replace('.', ','), out decimalValue))
                {
                    return decimalValue;
                }
                else
                {
                    return 0;
                }
            }
        }

        public static double ToDouble(this string str)
        {
            double doubleValue = 0;
            if (double.TryParse(str.Replace(',', '.'), out doubleValue))
            {
                return doubleValue;
            }
            else
            {
                if (double.TryParse(str.Replace('.', ','), out doubleValue))
                {
                    return doubleValue;
                }
                else
                {
                    return 0;
                }
            }
        }

        public static float ToFloat(this string str)
        {
            float floatValue = 0;
            if (float.TryParse(str.Replace(',', '.'), out floatValue))
            {
                return floatValue;
            }
            else
            {
                if (float.TryParse(str.Replace('.', ','), out floatValue))
                {
                    return floatValue;
                }
                else
                {
                    return 0;
                }
            }
        }

        public static Uri ToUri(this string str)
        {
            Uri result = new Uri("http://last.fm/");
            Uri.TryCreate(str, UriKind.Absolute, out result);
            return result;
        }

        public static string URLEncode(this string str)
        {
            return Uri.EscapeDataString(str);
        }

        public static string HTMLEncodeSpecialChars(this string str)
        {
            return HttpUtility.HtmlEncode(str);
        }
        public static string HTMLDecodeSpecialChars(this string str)
        {
            return HttpUtility.HtmlDecode(str);
        }

        public static string ToMD5(this string str)
        {
            MD5CryptoServiceProvider mD5CryptoServiceProvider = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(str);
            byteArray = mD5CryptoServiceProvider.ComputeHash(byteArray);
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            foreach (byte b in byteArray)
            {
                sb.Append(b.ToString("x2", CultureInfo.InvariantCulture).ToLowerInvariant());
            }
            return sb.ToString();
        }
    }
}
